<?php
/**
 * Description: 用户行为提交
 * User: devforma
 * Date: 16/3/7
 * Time: 14:29
 */

class UserAction implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {

        $acceptType = array(
            'do_topic',
            'do_interview',
            'do_politic',
            'collect_topic',
            'collect_interview',
            'collect_politic',
            'cancel_topic',
            'cancel_interview',
            'cancel_politic',
            'correct_topic',
            'hot_interview'
        );

        if (validateUser($queryArgs) !== 1 || empty($queryArgs['action_type']) || !in_array($queryArgs['action_type'], $acceptType)) {
            return genOutput(1000);
        }

        switch($queryArgs['action_type']) {
            case 'do_topic': //每日一题作答
                if (empty($queryArgs['topic_id']) || !filter_var($queryArgs['topic_id'], FILTER_VALIDATE_INT) || empty($queryArgs['answer'])) {
                    return genOutput(1000);
                }

                $this->saveTopicAnswer($queryArgs['user_id'], $queryArgs['topic_id'], $queryArgs['answer'], $queryArgs['is_right']);
                break;

            case 'do_interview': //每日一面作答
                if (empty($queryArgs['question_id']) || empty($queryArgs['filename']) || empty($queryArgs['duration'])) {
                    return genOutput(1000);
                }

                $this->saveInterviewAnswer($queryArgs['user_id'], $queryArgs['question_id'], $queryArgs['filename'], $queryArgs['duration']);
                break;

            case 'do_politic': //时政热点阅读
                if (empty($queryArgs['politic_id']) || !filter_var($queryArgs['politic_id'], FILTER_VALIDATE_INT)) {
                    return genOutput(1000);
                }

                $this->savePoliticRead($queryArgs['user_id'], $queryArgs['politic_id']);
                break;

            case 'collect_topic': //收藏每日一题
                if (empty($queryArgs['topic_id']) || !filter_var($queryArgs['topic_id'], FILTER_VALIDATE_INT)) {
                    return genOutput(1000);
                }

                $this->collectTopic($queryArgs['user_id'], $queryArgs['topic_id']);
                break;

            case 'collect_interview': //收藏每日一面
                if (empty($queryArgs['question_id']) || !filter_var($queryArgs['question_id'], FILTER_VALIDATE_INT)) {
                    return genOutput(1000);
                }

                $this->collectInterview($queryArgs['user_id'], $queryArgs['question_id']);
                break;

            case 'collect_politic': //收藏时政热点
                if (empty($queryArgs['politic_id']) || !filter_var($queryArgs['politic_id'], FILTER_VALIDATE_INT)) {
                    return genOutput(1000);
                }

                $this->collectPolitic($queryArgs['user_id'], $queryArgs['politic_id']);

                break;

            case 'cancel_topic': //取消收藏每日一题
                if (empty($queryArgs['topic_id']) || !filter_var($queryArgs['topic_id'], FILTER_VALIDATE_INT)) {
                    return genOutput(1000);
                }

                $this->cancelCollectTopic($queryArgs['user_id'], $queryArgs['topic_id']);
                break;

            case 'cancel_interview': //取消收藏每日一面
                if (empty($queryArgs['question_id']) || !filter_var($queryArgs['question_id'], FILTER_VALIDATE_INT)) {
                    return genOutput(1000);
                }

                $this->cancelCollectInterview($queryArgs['user_id'], $queryArgs['question_id']);
                break;

            case 'cancel_politic': //取消收藏时政热点
                if (empty($queryArgs['politic_id']) || !filter_var($queryArgs['politic_id'], FILTER_VALIDATE_INT)) {
                    return genOutput(1000);
                }

                $this->cancelCollectPolitic($queryArgs['user_id'], $queryArgs['politic_id']);

                break;

            case 'correct_topic': //删除每日一题错题
                if (empty($queryArgs['topic_id']) || !filter_var($queryArgs['topic_id'], FILTER_VALIDATE_INT)) {
                    return genOutput(1000);
                }

                $this->correctTopic($queryArgs['user_id'], $queryArgs['topic_id']);
                break;

            case 'hot_interview': //每日一面点赞
                if (empty($queryArgs['record_id']) || !filter_var($queryArgs['record_id'], FILTER_VALIDATE_INT)) {
                    return genOutput(1000);
                }

                $this->hotInterview($queryArgs['record_id']);
                break;

            default:
        }

        return genOutput(1);
    }


    /**
     * 每日一面点赞
     *
     * @param $recordId
     */
    private function hotInterview($recordId) {
        global $cmsdb;

        $cmsdb->query('UPDATE `data_interview_users` SET hot=hot+1 WHERE `id`='.$recordId);
    }

    /**
     * 用户删除每日一题错题
     *
     * @param $userId
     * @param $topicId
     */
    private function correctTopic($userId, $topicId) {
        global $cmsdb;

        $exists = $cmsdb->getVar('SELECT `id` FROM `relationship_user_topic` WHERE `user_id`=:uid AND `topic_id`=:tid',
            array(':uid' => $userId, ':tid' => $topicId));

        if ($exists) {
            $cmsdb->update('relationship_user_topic',
                array('is_wrong' => 2),
                array('id' => $exists)
            );
        }
    }

    /**
     * 用户取消收藏每日一题
     *
     * @param $userId
     * @param $topicId
     */
    private function cancelCollectTopic($userId, $topicId) {
        global $cmsdb;

        $exists = $cmsdb->getVar('SELECT `id` FROM `relationship_user_topic` WHERE `user_id`=:uid AND `topic_id`=:tid',
            array(':uid' => $userId, ':tid' => $topicId));

        if ($exists) {
            $cmsdb->update('relationship_user_topic',
                array('is_store' => 0),
                array('id' => $exists)
            );
        }
    }

    /**
     * 用户取消收藏每日一面
     *
     * @param $userId
     * @param $questionId
     */
    private function cancelCollectInterview($userId, $questionId) {
        global $cmsdb;

        $exists = $cmsdb->getVar('SELECT `id` FROM `data_interview_users` WHERE `uid`=:uid AND `qid`=:qid',
            array(':uid' => $userId, ':qid' => $questionId));

        if ($exists) {
            $cmsdb->update('data_interview_users',
                array('is_collected' => 0),
                array('id' => $exists)
            );
        }
    }

    /**
     * 用户取消收藏时政热点
     *
     * @param $userId
     * @param $politicId
     */
    private function cancelCollectPolitic($userId, $politicId) {
        global $cmsdb;

        $exists = $cmsdb->getVar('SELECT `id` FROM `dl_user_politic` WHERE `user_id`=:uid AND `politic_id`=:pid',
            array(':uid' => $userId, ':pid' => $politicId));

        if ($exists) {
            $cmsdb->update('dl_user_politic',
                array('is_collected' => 0),
                array('id' => $exists)
            );
        }
    }


    /**
     * 用户收藏时政热点
     *
     * @param $userId
     * @param $politicId
     */
    private function collectPolitic($userId, $politicId) {
        global $cmsdb;

        $exists = $cmsdb->getVar('SELECT `id` FROM `dl_user_politic` WHERE `user_id`=:uid AND `politic_id`=:pid',
            array(':uid' => $userId, ':pid' => $politicId));

        $date = date('Y-m-d H:i:s');

        if (!$exists) {
            $cmsdb->insert('dl_user_politic',
                array(
                    'user_id' => $userId,
                    'politic_id' => $politicId,
                    'is_collected' => 1,
                    'read_time' => $date,
                    'collect_time' => $date
                )
            );
        } else {
            $cmsdb->update('dl_user_politic',
                array(
                    'is_collected' => 1,
                    'collect_time' => $date
                ),
                array(
                    'id' => $exists
                )
            );
        }
    }


    /**
     * 用户阅读时政热点
     *
     * @param $userId
     * @param $politicId
     */
    private function savePoliticRead($userId, $politicId) {
        global $cmsdb;

        $exists = $cmsdb->getVar('SELECT `id` FROM `dl_user_politic` WHERE `user_id`=:uid AND `politic_id`=:pid',
            array(':uid' => $userId, ':pid' => $politicId));

        if (!$exists) {
            $cmsdb->insert('dl_user_politic',
                array(
                    'user_id' => $userId,
                    'politic_id' => $politicId,
                    'is_collected' => 0,
                    'read_time' => date('Y-m-d H:i:s')
                )
            );
        }
    }

    /**
     * 用户每日一面作答
     *
     * @param $userId
     * @param $questionId
     * @param $filename
     * @param $duration
     */
    private function saveInterviewAnswer($userId, $questionId, $filename, $duration) {
        global $cmsdb;

        $exists = $cmsdb->getVar('SELECT `id` FROM `data_interview_users` WHERE `uid`=:uid AND `qid`=:qid',
            array(':uid' => $userId, ':qid' => $questionId));

        if ($exists) {
            $cmsdb->update('data_interview_users',
                array(
                    'duration' => $duration,
                    'filename' => $filename,
                    'createtime' => date('Y'),
                    'status' => 1,
                    'hot' => 0
                ),
                array(
                    'id' => $exists
                )
            );
        } else {
            $cmsdb->insert('data_interview_users',
                array(
                    'uid' => $userId,
                    'qid' => $questionId,
                    'filename' => $filename,
                    'hot' => 0,
                    'duration' => $duration,
                    'createtime' => date('Y'),
                    'status' => 1,
                    'is_collected' => 0
                )
            );
        }
    }

    /**
     * 用户每日一面收藏
     *
     * @param $userId
     * @param $questionId
     */
    private function collectInterview($userId, $questionId) {
        global $cmsdb;

        $exists = $cmsdb->getVar('SELECT `id` FROM `data_interview_users` WHERE `uid`=:uid AND `qid`=:qid',
            array(':uid' => $userId, ':qid' => $questionId));

        if ($exists) {
            $cmsdb->update('data_interview_users',
                array(
                    'is_collected' => 1,
                    'collect_time' => date('Y-m-d H:i:s')
                ),
                array(
                    'id' => $exists
                )
            );
        } else {
            $cmsdb->insert('data_interview_users',
                array(
                    'uid' => $userId,
                    'qid' => $questionId,
                    'filename' => '',
                    'hot' => 0,
                    'duration' => 0,
                    'createtime' => 0,
                    'status' => 0,
                    'is_collected' => 1,
                    'collect_time' => date('Y-m-d H:i:s')
                )

            );
        }
    }


    /**
     * 用户每日一题作答
     *
     * @param $userId
     * @param $topicId
     * @param $answer
     * @param $isRight
     */
    private function saveTopicAnswer($userId, $topicId, $answer, $isRight) {
        global $cmsdb;

        $exists = $cmsdb->getVar('SELECT `id` FROM `relationship_user_topic` WHERE `user_id`=:uid AND `topic_id`=:tid',
            array(':uid' => $userId, ':tid' => $topicId));

        $date = date('Y-m-d H:i:s');

        if ($exists) {
            $cmsdb->update('relationship_user_topic',
                array(
                    'is_wrong' => !($isRight === 'true' || $isRight === '1'),
                    'user_answer' => $answer,
                    'wrong_time' => $date
                ),
                array(
                    'id' => $exists
                )
            );
        } else {
            $cmsdb->insert('relationship_user_topic',
                array(
                    'user_id' => $userId,
                    'topic_id' => $topicId,
                    'is_store' => 0,
                    'is_wrong' => !($isRight === 'true' || $isRight === '1'),
                    'wrong_time' => $date,
                    'user_answer' => $answer
                )
            );
        }
    }

    /**
     * 用户每日一题收藏
     *
     * @param $userId
     * @param $topicId
     */
    private function collectTopic($userId, $topicId) {
        global $cmsdb;

        $exists = $cmsdb->getVar('SELECT `id` FROM `relationship_user_topic` WHERE `user_id`=:uid AND `topic_id`=:tid',
            array(':uid' => $userId, ':tid' => $topicId));

        $date = date('Y-m-d H:i:s');

        if ($exists) {
            $cmsdb->update('relationship_user_topic',
                array(
                    'is_store' => 1,
                    'store_time' => $date
                ),
                array(
                    'id' => $exists
                )
            );
        } else {
            $cmsdb->insert('relationship_user_topic',
                array(
                    'user_id' => $userId,
                    'topic_id' => $topicId,
                    'is_store' => 1,
                    'is_wrong' => 0,
                    'store_time' => $date
                )
            );
        }
    }
}